package net.hn.hnms.biz.emgcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.hn.hnms.biz.emgcy.domain.AllocationRecordPo;
import net.hn.hnms.biz.emgcy.domain.DutyRecord;
import net.hn.hnms.biz.emgcy.domain.DutyRecordPo;
import net.hn.hnms.biz.emgcy.mapper.DutyRecordMapper;
import net.hn.hnms.biz.emgcy.service.AllocationRecordService;
import net.hn.hnms.biz.emgcy.service.DutyRecordService;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: baijiajun
 * @Date: 2022/9/14
 */
@Service
public class DutyRecordServiceImpl implements DutyRecordService {
    @Autowired
    private DutyRecordMapper dutyRecordMapper;
    @Autowired
    private AllocationRecordService allocationRecordService;
    @Autowired
    private RemoteDictService remoteDictService;

    @Override
    public List list(DutyRecordPo dutyRecordPo) {
        QueryWrapper<DutyRecordPo> wrapper = new QueryWrapper<>();
        wrapper.eq("duty_id", dutyRecordPo.getDutyId());
        List<DutyRecordPo> dutyRecords = dutyRecordMapper.selectList(wrapper);
        ArrayList<DutyRecord> list = new ArrayList<>();
        if(dutyRecords.size() == 1){
            DutyRecordPo dutyRecord1 =dutyRecords.get(0);
            String label = remoteDictService.selectDictLabel("event_type", dutyRecord1.getEventType());
            String s=null;
            String str="发生了"+label+","
                    +dutyRecord1.getAlarmContent()
                    +"伤亡情况:死亡" + dutyRecord1.getDeathNumber() + "人" +
                    "重伤/中毒:" + dutyRecord1.getMayhemNumber() + "人" +
                    "轻伤:" + dutyRecord1.getWoundNumber() + "人" +
                    "其他情况:"+dutyRecord1.getRemarks()+
                    ",事故等级:";
            Integer integer = Integer.valueOf(dutyRecord1.getEventLevel());
            if (Integer.valueOf(dutyRecord1.getEventLevel()) == 1) s = "I级(特大事故)";
            if (Integer.valueOf(dutyRecord1.getEventLevel()) == 2) s = "II级(重大事故)";
            if (Integer.valueOf(dutyRecord1.getEventLevel()) == 3) s = "III级(较大事故)";
            if (Integer.valueOf(dutyRecord1.getEventLevel()) == 4) s = "IV级(一般事故)";
            DutyRecord dutyRecord = new DutyRecord();
            dutyRecord.setCreateTime(dutyRecord1.getCreateTime());
            dutyRecord.setStr(str + s+"。");
            list.add(dutyRecord);
        }
        if (dutyRecords.size() > 1) {
            for (int i = 0; i < dutyRecords.size(); i++) {
                String str = "";
                String s = "";
                String o = "";
                if (i <= dutyRecords.size() - 2) {
                    if(!dutyRecords.get(i).getEventType().equalsIgnoreCase(dutyRecords.get(i+1).getEventType())){
                        String label = remoteDictService.selectDictLabel("event_type", dutyRecords.get(i).getEventType());
                        String label1 = remoteDictService.selectDictLabel("event_type", dutyRecords.get(i+1).getEventType());
                        str+="事故由"+label+"变更为"+label1+",";
                    }
                    if (!dutyRecords.get(i).getTitle().equalsIgnoreCase(dutyRecords.get(i + 1).getTitle())) {
                        str += "事故标题由:" + dutyRecords.get(i) + "变更为" + dutyRecords.get(i + 1).getTitle() + ",";
                    }
                    if (!dutyRecords.get(i).getAlarmContent().equalsIgnoreCase(dutyRecords.get(i + 1).getAlarmContent())) {
                        str += "事故详情由" + dutyRecords.get(i).getAlarmContent() + "变更为" + dutyRecords.get(i + 1).getAlarmContent() + ",";
                    }
                    if (dutyRecords.get(i).getDeathNumber() != dutyRecords.get(i + 1).getDeathNumber()) {
                        str += "死亡人数由" + dutyRecords.get(i).getDeathNumber() + "人变更为" + dutyRecords.get(i + 1).getDeathNumber() + "人,";
                    }
                    if (dutyRecords.get(i).getMayhemNumber() != dutyRecords.get(i + 1).getMayhemNumber()) {
                        str += "重伤/中毒人数由" + dutyRecords.get(i).getMayhemNumber() + "人变更为" + dutyRecords.get(i + 1).getMayhemNumber() + "人,";
                    }
                    if (dutyRecords.get(i).getWoundNumber() != dutyRecords.get(i + 1).getWoundNumber()) {
                        str += "轻伤人数由" + dutyRecords.get(i).getWoundNumber() + "人变更为" + dutyRecords.get(i + 1).getWoundNumber() + "人,";
                    }
                    if (!dutyRecords.get(i).getRemarks().equalsIgnoreCase(dutyRecords.get(i + 1).getRemarks())) {
                        str += "其他情况由" + dutyRecords.get(i).getRemarks() + "变更为" + dutyRecords.get(i + 1).getRemarks() + ",";
                    }
                    if (!dutyRecords.get(i).getEventLevel().equalsIgnoreCase(dutyRecords.get(i + 1).getEventLevel())) {
                        if (Integer.valueOf(dutyRecords.get(i).getEventLevel()) == 1) s = "I级(特大事故)";
                        if (Integer.valueOf(dutyRecords.get(i).getEventLevel()) == 2) s = "II级(重大事故)";
                        if (Integer.valueOf(dutyRecords.get(i).getEventLevel()) == 3) s = "III级(较大事故)";
                        if (Integer.valueOf(dutyRecords.get(i).getEventLevel()) == 4) s = "IV级(一般事故)";
                        if (Integer.valueOf(dutyRecords.get(i + 1).getEventLevel()) == 1) o = "I级(特大事故)";
                        if (Integer.valueOf(dutyRecords.get(i + 1).getEventLevel()) == 2) o = "II级(重大事故)";
                        if (Integer.valueOf(dutyRecords.get(i + 1).getEventLevel()) == 3) o = "III级(较大事故)";
                        if (Integer.valueOf(dutyRecords.get(i + 1).getEventLevel()) == 4) o = "IV级(一般事故)";
                        str += "事故等级由:" + s + "变更为" + o + ",";
                    }
                    if (!dutyRecords.get(i).getEditorId().equalsIgnoreCase(dutyRecords.get(i + 1).getEditorId())) {
                        str += "编辑人由:" + dutyRecords.get(i).getEditorId() + "变更为" + dutyRecords.get(i + 1).getEditorId() + ",";
                    }
                    if (!dutyRecords.get(i).getTelephone().equalsIgnoreCase(dutyRecords.get(i + 1).getTelephone())) {
                        str += "联系电话由:" + dutyRecords.get(i).getTelephone() + "变更为" + dutyRecords.get(i + 1).getTelephone() + ",";
                    }

                    DutyRecord dutyRecordDTO = new DutyRecord();
                    dutyRecordDTO.setCreateTime(dutyRecords.get(i + 1).getCreateTime());
                    if (str.length() > 0) {
                        dutyRecordDTO.setStr(str.substring(0, str.length() - 1) + "。");
                    } else {
                        dutyRecordDTO.setStr("没有发生变更");
                    }
                    list.add(dutyRecordDTO);
                }
            }
        }
        QueryWrapper<AllocationRecordPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("duty_id", dutyRecordPo.getDutyId());
        List<AllocationRecordPo> list1 = allocationRecordService.list(queryWrapper);
        List<DutyRecord> collect = list1.stream().map(allocationRecord -> {
            DutyRecord dutyRecord = new DutyRecord();
            dutyRecord.setCreateTime(allocationRecord.getAllocationTime());
            if (allocationRecord.getAllocationType() == 1) {
                dutyRecord.setStr("调度队伍成员有:" + allocationRecord.getAllocationObject() +
                        ",调度详情:" + allocationRecord.getAllocationDetail());
            }
            if (allocationRecord.getAllocationType() == 2) {
                dutyRecord.setStr("调度专家成员有:" + allocationRecord.getAllocationObject() +
                        ",调度详情:" + allocationRecord.getAllocationDetail());
            }
            if (allocationRecord.getAllocationType() == 3) {
                dutyRecord.setStr("调度物资有:" + allocationRecord.getAllocationObject() +
                        ",调度详情:" + allocationRecord.getAllocationDetail());
            }
            return dutyRecord;
        }).collect(Collectors.toList());
        collect.forEach(record -> {
            list.add(record);
        });
        List<DutyRecord> collect1 = list.stream().sorted((o1, o2) -> {
            Long aLong = null;
            try {
                aLong = StringDatetoLong(o1.getCreateTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Long aLong1 = null;
            try {
                aLong1 = StringDatetoLong(o2.getCreateTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return (int) (aLong - aLong1);
        }).collect(Collectors.toList());
        return list;
    }


    @Override
    public Boolean add(DutyRecordPo dutyRecordPo) {
        int insert = dutyRecordMapper.insert(dutyRecordPo);
        if(insert>0){
            return true;
        }
        return false;
    }

    public Long StringDatetoLong(String str) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse(str);
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse);
        long time=cal.getTimeInMillis();
        return time;
    }
}
